package fr.esgi.pages;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.esgi.classes.Personne;
import fr.esgi.donnees.Database;
import fr.esgi.utils.Utils;


public class ValidationInscriptionAdministration extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ValidationInscriptionAdministration() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Utils u=new Utils();
		String nom=request.getParameter("lastname");
		String prenom=request.getParameter("firstname");
		String mail=request.getParameter("mail");
		String mdp=request.getParameter("mdp");
		String role=request.getParameter("role");
		if(!u.checkNom(nom)||!u.checkSize(nom)){
			request.setAttribute("erreur", "probleme sur le nom.");
			getServletContext().getRequestDispatcher("/jsp/pages/inscription.jsp").forward(request, response);
		}
		else if (!u.checkNom(prenom)||!u.checkSize(prenom)){
			request.setAttribute("erreur", "probleme sur le prenom.");
			getServletContext().getRequestDispatcher("/jsp/pages/inscription.jsp").forward(request, response);
		}
		else if(!u.checkMail(mail)||!u.checkSize(mail)){
			request.setAttribute("erreur", "probleme sur le mail.");
			getServletContext().getRequestDispatcher("/jsp/pages/inscription.jsp").forward(request, response);
		}
		else if(!u.checkNom(mdp)||!u.checkSize(mdp)){
			request.setAttribute("erreur", "probleme sur le mot de passe.");
			getServletContext().getRequestDispatcher("/jsp/pages/inscription.jsp").forward(request, response);
		}		
		else {
			Personne e=new Personne(nom, prenom, role, mail,mdp);
			Database d=new Database();
			try {
				d.insertAdministration(e);
			} catch (ClassNotFoundException | SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			getServletContext().getRequestDispatcher("/jsp/pages/accueil.jsp").forward(request, response);
			
		}
	}

}
